<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="申请时间范围" prop="procureRangeTime" label-width="100px">
        <el-date-picker v-model="procureRangeTime" type="datetimerange" :picker-options="pickerOptions"
          value-format="yyyy-MM-dd HH:mm:ss" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
          align="right">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" @click="handleAdd"
           v-hasRole="['department']" v-if = "!$store.getters.roles.includes('admin')">新增</el-button> 
           <!-- v-hasPermi="['warehousemanager:procure:add']" -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['warehousemanager:procure:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="procureList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" width="55" type="index" :index.sync="indexMethod" />
      <el-table-column label="编号" align="center" prop="id" v-if="false" />
      <el-table-column label="易耗品名称" align="center" prop="itemName" />
      <el-table-column label="数量" align="center" prop="itemNum" />
      <el-table-column label="申请人" align="center" prop="applicant" />
      <el-table-column label="申请时间" align="center" prop="applicationTime" width="180" />
      <el-table-column v-hasRole="['admin']" v-if="$store.getters.name == 'admin' ? true : false" label="操作"
        align="center" prop="status" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['warehousemanager:check:delete']">删除</el-button>
        </template><!-- 管理员才能看到 -->
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="55%" append-to-body>
      <div style="display:flex;text-align:center;justify-content: center;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="width: 70%;">
          <el-form-item label="物品名称" prop="itemName" :span="12" :offset="6">
            <el-input v-model="form.itemName" placeholder="请输入易耗品名称" />
          </el-form-item>
          <el-form-item label="申请数量" prop="itemNum">
            <el-input v-model="form.itemNum" placeholder="请输入申请数量" />
          </el-form-item>
          <el-form-item label="申请人" prop="applicant">
            <el-input v-model="form.applicant" placeholder="请输入申请人" :disabled="true" />
          </el-form-item>
          <!-- <el-form-item label="申请时间" prop="applicationTime">
            <el-date-picker clearable v-model="form.applicationTime" type="date" value-format="yyyy-MM-dd hh:mm:ss"
              placeholder="请选择申请时间">
            </el-date-picker>
          </el-form-item> -->
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcure, listProcureByQuery, delProcure, addProcure} from "@/api/warehousemanager/procure";

export default {
  name: "Procure",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购表格数据
      procureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        itemNum: null,
        itemUnit: null,
        applicant: null,
        applicationTime: null
      },
      //日期时间选择器
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }],
      },
      //时间范围
      procureRangeTime: null,
      rangeTimeForExport: {
        begin: null,
        end: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        itemName: [
            { required: true, message: '请输入物品名称', trigger: 'blur' },
            { pattern: /^[A-Za-z0-9\u4e00-\u9fa5]+$/, message: '不允许输入空格等特殊符号', trigger: 'blur' },
          ],
          itemNum: [
            { required: true, message: '请输入物品数量' },
            {pattern:/^[1-9]\d*$/,message:'数量必须为数字值'},
            { required: true, trigger: "blur" }
          ]
          // applicationTime: [
          //   { required: true, message: '请选择申请日期', trigger: 'change' }
          // ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //自增序号
    indexMethod(index) {
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
    },
    /** 查询采购列表 */
    getList() {
      this.loading = true;
      listProcure(this.queryParams).then(response => {
        this.procureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 筛选列表----by时间范围 */
    getListByQuery() {
      this.loading = true;
      this.rangeTimeForExport.begin = this.procureRangeTime[0];
      this.rangeTimeForExport.end = this.procureRangeTime[1];
      listProcureByQuery({ ...this.rangeTimeForExport }).then(response => {
        this.procureList = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemName: null,
        itemNum: null,
        itemUnit: null,
        applicant: null,
        applicationTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.procureRangeTime == null) {
        this.getList();
      } else {
        this.getListByQuery();
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.procureRangeTime = null;
      this.rangeTimeForExport.begin = null;
      this.rangeTimeForExport.end = null;
      this.handleQuery();
    },
    /** 新增采购物品操作 */
    handleAdd() {
      this.reset();
      this.form.applicant = this.$store.getters.name;
      this.open = true;
      this.title = "添加采购物品";
    },
    /** 提交新增采购物品操作按钮 */
    submitForm() {
      this.$refs["form"].validate(valid=>{
        if (valid){
          addProcure(this.form).then(res =>{
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.reset();
              this.handleQuery();
          })
        }
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehousemanager/procure/export', {
        ...this.rangeTimeForExport
      }, `procure_${new Date().getTime()}.xlsx`)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const procureIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + procureIds + '"的数据项？').then(function () {
        return delProcure(procureIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
